package com.Set;
/**
 * 了解hashSet去重的机制
 * 重写hascode和equal方法实现对象内容相同就判定这里两个对象相同，符合实际开发
 */

import java.util.HashSet;

public class SetDemo3 {
    public static void main(String[] args) {
        //HashSet的去重的默认机制先判断两个对象的哈希值是否一样，不一样才判断对象里面的内容是否一样
        Student student1=new Student("lingyi",18,'男');
        Student student2=new Student("lingyi",18,'男');
        Student student3=new Student("ting",20,'女');
        System.out.println(student1.equals(student2));

        HashSet<Student>  sets=new HashSet<>();
        sets.add(student1);
        sets.add(student2);
        sets.add(student3);
        System.out.println(sets);
        //[Student{name='lingyi', age=18, sex=男}, Student{name='ting', age=20, sex=女}, Student{name='lingyi', age=18, sex=男}]

        //在Student类中重写hashcode和equal方法，解决
        //[Student{name='ting', age=20, sex=女}, Student{name='lingyi', age=18, sex=男}]


    }
}
